package shellSort;

/**
 * Created with IntelliJ IDEA.
 * Description: 希尔排序：缩小增量算法
 *                  1.分组
 *                  2.组内直接插入排序
 * User: 86187
 * Date: 2022-08-09
 * Time: 22:02
 */

public class MyShellSort {

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for (; j >= 0; j -= gap) {
                if(array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    public static void shellSort(int[] array) {
        /*
        int[] drr = {5,2,1};
        for (int i = 0; i < drr.length; i++) {
            shell(array,drr[i]);
        }
        */
        int gap = array.length;
        while(gap > 1) {
            shell(array,gap);
            gap = gap >>> 1;
        }
        shell(array,1);
    }
}
